package org.example.interceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @ClassName InterceptorConfig
 * @Description: TODO
 * @Author ChenYuan
 * @Date 2020/12/9
 * @Version V1.0
 **/
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    /**
     * 跨域配置
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .maxAge(1728000)
                .allowCredentials(true)
                .allowedHeaders("Origin","X-Requested-With","Content-Type","Accept");
    }

    /**
     * 拦截器配置
     * InterceptorRegistry.excludePathPatterns 排除的路径比自定义的拦截器里的排除的路径优先级高 是最外部的路径过滤
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new JwtInterceptor())
                // 拦截的请求 /service/**
                .addPathPatterns("/**")
                // 不拦截的请求  如登录接口
                .excludePathPatterns("/customer/**");
                //.excludePathPatterns("/test/**");

    }
}
